package com.xctech.yace.util;

import com.google.common.collect.Maps;
import com.xctech.etl.core.pkgscanner.*;
import com.xctech.yace.conparator.StringComparator;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.*;

@Slf4j
public class ReadFiledXML {

    @SneakyThrows
    public static Map<String,List<String>> readFiledsFile() {
        Map<String, List<String>> result = new HashMap<>();

        String filePath = DirUtil.getOrgPath()+"conf/topics.xml";
        FileInputStream inputStream = new FileInputStream(filePath);

        SAXReader reader = new SAXReader();
        Document dom = reader.read(inputStream);
        Element root = dom.getRootElement();

        for(Iterator i = root.elementIterator("topics");i.hasNext();) {
            Element topics = (Element) i.next();
            String key = topics.attributeValue("prop");

            List<String> subTopics = new ArrayList<>();
            for(Iterator iterator = topics.elementIterator("topic");iterator.hasNext();) {
                subTopics.add(((Element)iterator.next()).getText().trim());
            }
            result.put(key,subTopics);
        }
        return result;
    }

    @SneakyThrows
    public static Map<String,String> readModelFile(String path){
        Map<String, String> result =
                ScanUtils.scanAndLoadFiles(path,
                        ResourceType.SQL_FILE, true);
        //按照key的String排序
        Map<String,String> res = new TreeMap<>(new StringComparator());
        res.putAll(result);
        return res;
    }

    @SneakyThrows
    public static Map<String,Map<String,String>> readFileNode(String path){
        List<String> nodeList = new ArrayList<>();
        traverseFolders(path,nodeList,ResourceType.SQL_FILE.getTypeString());
        Map<String,Map<String,String>> result = Maps.newHashMap();
        nodeList.stream().forEach(node->{
            try {
                Map<String, String> r1 = ScanUtils.scanAndLoadFiles(node,ResourceType.SQL_FILE, true);
                //按照key的String排序
                Map<String,String> res = new TreeMap<>(new StringComparator());
                res.putAll(r1);
                result.put(node,res);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return result;
    }

    public static void traverseFolders(String path,List<String> result, String suffix) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                File[] var6 = files;
                int var7 = files.length;
                for(int var8 = 0; var8 < var7; ++var8) {
                    File file2 = var6[var8];
                    if (file2.isDirectory()) {
                        traverseFolders(file2.getPath(),result,suffix);
                    }else {
                        result.add(file2.getParent());
                    }
                }
            }
        }
    }


}
